package com.syh.jn.scm.domain.supplier.entity.finance;

import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.Downstream;
import com.syh.jn.scm.domain.supplier.entity.base.logistics.Logistics;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.Upstream;
import com.syh.jn.scm.domain.supplier.repository.base.LogisticsReadRepository;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamReadRepository;
import com.syh.jn.scm.domain.supplier.repository.base.upstream.UpstreamReadRepository;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.supplier.finance.enums.BusinessAccountRecordEnums;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * 单位应收付记录汇总表
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class BusinessAccountSummary extends AbstractSimpleAggregate<Long> {
  public BusinessAccountSummary(Downstream downstream) {
    this.customerId = downstream.getId();
    this.customerType = Short.valueOf("1");
    this.systemCreate();
    this.businessEntityId = downstream.getBusinessEntityId();
  }

  public BusinessAccountSummary(Upstream upstream) {
    this.customerId = upstream.getId();
    this.customerType = Short.valueOf("0");
    this.systemCreate();
    this.businessEntityId = upstream.getBusinessEntityId();
  }

  public BusinessAccountSummary(Logistics logistics) {
    this.customerId = logistics.getId();
    this.customerType = Short.valueOf("2");
    this.systemCreate();
    this.businessEntityId = logistics.getBusinessEntityId();
  }

  public void updateBusinessAccountSummary(BusinessAccountRecord businessAccountRecord) {
    switch (businessAccountRecord.getCustomerType().intValue()) {
      //供应商
      case 0:
        this.updateUpStreamBusinessAccountSummary(businessAccountRecord);
        break;
      //采购商
      case 1:
        this.updateDownsStreamBusinessAccountSummary(businessAccountRecord);
        break;
      //物流
      case 2:
        this.updateLogisticsBusinessAccountSummary(businessAccountRecord);
        break;
        default:
          throw new BusinessException("类型错误");
    }
  }

  private void updateUpStreamBusinessAccountSummary(BusinessAccountRecord businessAccountRecord) {
    //如果是付款单，增加已付款金额
    if (businessAccountRecord.getOriginalDocumentType().equals(BusinessAccountRecordEnums.Type.PAYMENT_BILL.getStatus())) {
      this.paymentAmount = this.paymentAmount.add(businessAccountRecord.getRePayAmount());
      return;
    }
    switch (businessAccountRecord.getOriginalDocumentType().intValue()) {
      //采购退货单
      case 2:
      //采购单
      case 5:
      //费用单
      case 7:
      //收入单
      case 3:
      //优惠金额
      case 13:
      case 14:
      //采购单其他费用
      case 16:
        //应付金额增加
        this.paymentTotalAmount = this.paymentTotalAmount.add(businessAccountRecord.getChangeAmount());
        break;
        default:
          throw new BusinessException("此时供应商不应该产生应付变动！");
    }
  }

  private void updateDownsStreamBusinessAccountSummary(BusinessAccountRecord businessAccountRecord) {
    //如果是收款单，增加已收款金额
    if (businessAccountRecord.getOriginalDocumentType().equals(BusinessAccountRecordEnums.Type.RECEIPT_BILL.getStatus())) {
      this.receiveAmount = this.receiveAmount.add(businessAccountRecord.getRePayAmount());
      return;
    }
    switch (businessAccountRecord.getOriginalDocumentType().intValue()) {
      //销售单
      case 1:
      //收入单
      case 3:
        //销售退货单
      case 6:
        //费用单
      case 7:
        //优惠金额
      case 11:
      case 12:
      //销售单其他费用
      case 15:
        //应收金额增加
        this.receiveTotalAmount = this.receiveTotalAmount.add(businessAccountRecord.getChangeAmount());
        break;
      default:
        throw new BusinessException("此时客户不应该产生应付变动！");
    }
  }

  private void updateLogisticsBusinessAccountSummary(BusinessAccountRecord businessAccountRecord) {
    //如果是付款单，增加已付款金额
    if (businessAccountRecord.getOriginalDocumentType().equals(BusinessAccountRecordEnums.Type.PAYMENT_BILL.getStatus())) {
      this.paymentAmount = this.paymentAmount.add(businessAccountRecord.getRePayAmount());
      return;
    }
    switch (businessAccountRecord.getOriginalDocumentType().intValue()) {
        //收入单
      case 3:
        //费用单
      case 7:
        //销售物流单
      case 9:
        //采购退货物流单
      case 10:
        //应付金额增加
        this.paymentTotalAmount = this.paymentTotalAmount.add(businessAccountRecord.getChangeAmount());
        break;
      default:
        throw new BusinessException("此时物流不应该产生应付变动！");
    }
  }


  /**
   * 系统默认创建
   */
  private void systemCreate() {
    this.receiveAmount = BigDecimal.valueOf(0);
    this.receiveTotalAmount = BigDecimal.valueOf(0);
    this.paymentAmount = BigDecimal.valueOf(0);
    this.paymentTotalAmount = BigDecimal.valueOf(0);
    this.uuid = UUID.randomUUID().toString();
  }

  /**
   * 客户id
   */
  private Long customerId;
  /**
   * 客户类型: 0上游, 1下游, 2物流
   */
  private Short customerType;
  /**
   * 应收金额
   */
  private BigDecimal receiveTotalAmount;
  /**
   * 应付金额
   */
  private BigDecimal paymentTotalAmount;
  /**
   * 已收金额
   */
  private BigDecimal receiveAmount;
  /**
   * 已付金额
   */
  private BigDecimal paymentAmount;
  /**
   * 归属端id
   */
  private Long businessEntityId;
}
